import re
import urllib

from com.mitbbs.util import firefox
from com.mitbbs.tools import common


RECEIVE_URL = 'http://www.mitbbs.com/mitbbs_mailbox.php?option=receive&path=r'
FORWARD_URL = 'http://www.mitbbs.com/mitbbs_bbsmailfwd.php'
DELETE_URL = 'http://www.mitbbs.com/mitbbs_bbsmailact.php'


def check_mail():
  count = 0
  index_txt = browser.open_url(RECEIVE_URL)
  pattern = re.compile("<input name=\"(file\d*)\" value=\"(.*?)\" type=\"checkbox\">.*?<a href=\"(/mitbbs_mailbox.php\?option=read&dir=r&num=\d*&title=.*?)\"", re.I | re.M | re.S)
  mails = {}
  for match in re.finditer(pattern, index_txt):
    url = 'http://www.mitbbs.com%s' % match.group(3)
    mail_content = browser.open_url(url)
    forward_mail(mail_content)
    mails[match.group(1)] = match.group(2)
    count += 1
  delete_mails(mails, index_txt)
  return count


def forward_mail(mail_content):
  pattern = re.compile("<form action=\"/mitbbs_bbsmailfwd.php\" method=\"post\">(.*?)</form>", re.I | re.M | re.S)
  m = re.search(pattern, mail_content)  
  form_txt = m.group(1)
  
  params = {}
  for m2 in re.finditer(r'<input.*?name="(.*?)".*?value="(.*?)"', form_txt):
    params[m2.group(1)] = m2.group(2)
  
  params['target'] = 'marmot101@gmail.com'
  params['big5'] = '0'
  params['noansi'] = '0'
  data = urllib.urlencode(params)  
  browser.open_url(FORWARD_URL, data)


def delete_mails(mails, index_txt):
  # delete emails
  params = {
              'act' : 'move',
              'act2' : 'delarea',
              'curbox' : 'r',
              'dir' : 'r',
              'moveto' : '',
              'moveto_up' : '',
              'movetobox' : '',
              'submit_mailact' : '',
              'type' : 'receive'
            }
  params.update(mails)
  
  pattern = re.compile("<form action=\"/mitbbs_bbsmailact.php\" method=\"post\" name=\"moveform\">.*?name=\"PREUTMPKEY\" value=\"(.*?)\".*?</form>", re.I | re.M | re.S)
  m = re.search(pattern, index_txt)
  bbs_key = m.group(1)
  params['PREUTMPKEY'] = bbs_key
  
  data = urllib.urlencode(params)
  browser.open_url(DELETE_URL, data)

  # empty garbage
  params.clear()
  params['PREUTMPKEY'] = bbs_key
  params['act'] = 'clear'
  params['submit_mailact'] = '' 
  data = urllib.urlencode(params)
  browser.open_url(DELETE_URL, data)

#############################################################################
# Deprecated, delete mails in index level is more efficient
#############################################################################
def delete_mail(mail_content):
  pattern = re.compile("<form action=\"/mitbbs_bbsmailact.php\" method=\"post\" name=\"del_mail_form\">(.*?)</form>", re.I | re.M | re.S)
  m = re.search(pattern, mail_content)  
  form_txt = m.group(1)
  
  params = {}
  for m2 in re.finditer(r'<input.*?name="(.*?)".*?value="(.*?)"', form_txt):
    params[m2.group(1)] = m2.group(2)
  
  del(params['add_badlist'])
  del(params['del'])
  del(params['add_friend'])
  del(params['reply'])
  params['submit_mailact'] = ''
  data = urllib.urlencode(params)
  browser.open_url(DELETE_URL, data)
  

############################################################################
# Main forwarder
############################################################################
browser = firefox.Firefox()
common.login(browser, '', '')

while check_mail() > 0:
  pass

common.logout(browser)